﻿/*---------------------------------------------------------------------------------
-----------------***DaLianQianYi***-----------------------
-----------------作者：大连仟亿科技------------------------------------------------------
-----------------联系作者：www.qianyikeji.com----------------------------------
-----------------时间：2024/7/15 14:05:12------------------------------------------
----------------------------------------------------------------------------------*/


using System;
using System.Text;
using System.Data;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Donal.Xms.Common;

namespace Donal.Xms.Web.qianyi.common
{
    public partial class spec_prices_list : Web.UI.ManagePage
    {
        protected int totalCount;
        protected int page;
        protected int pageSize;

        protected int isGoods;
        protected int isUser;
        protected int premium;
        protected int status;
        protected string keywords = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            this.isGoods = DTRequest.GetQueryInt("isGoods");
            this.isUser = DTRequest.GetQueryInt("isUser");
            this.premium = DTRequest.GetQueryInt("premium");
            this.status = DTRequest.GetQueryInt("status");
            this.keywords = DTRequest.GetQueryString("keywords");

            this.pageSize = GetPageSize(10); //每页数量
            if (!Page.IsPostBack)
            {
                ChkAdminLevel("spec_prices", DTEnums.ActionEnum.View.ToString()); //检查权限
                RptBind("id>0" + CombSqlTxt(keywords), "priority, id");
            }
        }

        #region 数据绑定=================================
        private void RptBind(string _strWhere, string _orderby)
        {
            this.page = DTRequest.GetQueryInt("page", 1);

            ddlIsGoods.SelectedValue = isGoods.ToString();
            ddlStatus.SelectedValue = status.ToString();
            ddlIsUser.SelectedValue = isUser.ToString();
            ddlPremium.SelectedValue = premium.ToString();

            txtKeywords.Text = this.keywords;
            BLL.spec_prices bll = new BLL.spec_prices();
            this.rptList.DataSource = bll.GetListView(this.pageSize, this.page, _strWhere, _orderby, out this.totalCount);
            this.rptList.DataBind();

            //绑定页码
            txtPageNum.Text = this.pageSize.ToString();
            string pageUrl = Utils.CombUrlTxt("spec_prices_list.aspx", "isGoods={0}&isUser={1}&status={2}&premium={3}&keywords={4}&page={5}", this.isGoods.ToString(), this.isUser.ToString(), this.status.ToString(), this.premium.ToString(), this.keywords, "__id__");
            PageContent.InnerHtml = Utils.OutPageList(this.pageSize, this.page, this.totalCount, pageUrl, 8);
        }
        #endregion

        #region 组合SQL查询语句==========================
        protected string CombSqlTxt(string _keywords)
        {
            StringBuilder strTemp = new StringBuilder();
            _keywords = _keywords.Replace("'", "");
            if (!string.IsNullOrEmpty(_keywords))
            {
                strTemp.Append(" and (user_group_title like  '%" + _keywords + "%' or goods_product_category_title like '%" + _keywords + "%')");
            }

            if (isGoods > 0)
            {
                strTemp.Append(" and is_goods =" + (isGoods - 1));
            }
            if (isUser > 0)
            {
                strTemp.Append(" and is_user =" + (isUser - 1));
            }
            if (status > 0)
            {
                strTemp.Append(" and status =" + (status - 1));
            }
            if (premium > 0)
            {
                strTemp.Append(" and price_type =" + (premium - 1));
            }

            return strTemp.ToString();
        }
        #endregion

        #region 返回每页数量=============================
        private int GetPageSize(int _default_size)
        {
            int _pagesize;
            if (int.TryParse(Utils.GetCookie("spec_prices_page_size", "Donal.XmsPage"), out _pagesize))
            {
                if (_pagesize > 0)
                {
                    return _pagesize;
                }
            }
            return _default_size;
        }
        #endregion

        private void Redirect()
        {
            Response.Redirect(Utils.CombUrlTxt("spec_prices_list.aspx", "isGoods={0}&isUser={1}&status={2}&premium={3}&keywords={4}", ddlIsGoods.SelectedValue, ddlIsUser.SelectedValue, ddlStatus.SelectedValue, ddlPremium.SelectedValue, txtKeywords.Text));
        }


        //关健字查询
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            Redirect();
        }

        //设置分页数量
        protected void txtPageNum_TextChanged(object sender, EventArgs e)
        {
            int _pagesize;
            if (int.TryParse(txtPageNum.Text.Trim(), out _pagesize))
            {
                if (_pagesize > 0)
                {
                    Utils.WriteCookie("spec_prices_page_size", "Donal.XmsPage", _pagesize.ToString(), 14400);
                }
            }
            Redirect();
        }

        //批量删除
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            ChkAdminLevel("spec_prices", DTEnums.ActionEnum.Delete.ToString()); //检查权限
            int sucCount = 0;
            int errorCount = 0;
            BLL.spec_prices bll = new BLL.spec_prices();
            for (int i = 0; i < rptList.Items.Count; i++)
            {
                int id = Convert.ToInt32(((HiddenField)rptList.Items[i].FindControl("hidId")).Value);
                CheckBox cb = (CheckBox)rptList.Items[i].FindControl("chkId");
                if (cb.Checked)
                {
                    if (bll.Delete(id))
                    {
                        sucCount += 1;
                    }
                    else
                    {
                        errorCount += 1;
                    }
                }
            }
            AddAdminLog(DTEnums.ActionEnum.Delete.ToString(), "删除成功" + sucCount + "条，失败" + errorCount + "条"); //记录日志
            JscriptMsg("删除成功" + sucCount + "条，失败" + errorCount + "条！", Utils.CombUrlTxt("spec_prices_list.aspx", "isGoods={0}&isUser={1}&status={2}&premium={3}&keywords={4}", ddlIsGoods.SelectedValue, ddlIsUser.SelectedValue, ddlStatus.SelectedValue, ddlPremium.SelectedValue, txtKeywords.Text));
        }


        protected void ddlStatus_SelectedIndexChanged(object sender, EventArgs e)
        {
            Redirect();
        }

        protected void ddlIsGoods_SelectedIndexChanged(object sender, EventArgs e)
        {
            Redirect();
        }

        protected void ddlIsUser_SelectedIndexChanged(object sender, EventArgs e)
        {
            Redirect();
        }

        protected void ddlPremium_SelectedIndexChanged(object sender, EventArgs e)
        {
            Redirect();
        }
    }
}

